/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'qrcode',
], function ($, common, ready, Collection, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'coupons',
                condition: {status: 'all'},
                model: {
                    url: _global.url.api + 'coupon',
                    event: {
                        '.item-online:click': 'online',
                        '.item-offline:click': 'offline',
                        '.item-creat_code:click': 'create_code',
                        '.item-qrcode:click': 'qrcode',
                        '.item-show:click': 'show',
                        '.item-hide:click': 'hide',
                    },
                    online: function (e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'coupon?is_open=1&id=' + this.get_data('id'),
                            type: 'put',
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.is_open = 1;
                                    _this.create_element();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },
                    offline: function (e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'coupon?is_open=0&id=' + this.get_data('id'),
                            type: 'put',
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.is_open = 0;
                                    _this.create_element();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },
                    show: function (e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'coupon?is_show=1&id=' + this.get_data('id'),
                            type: 'put',
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.is_show = 1;
                                    _this.create_element();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },
                    hide: function (e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'coupon?is_show=0&id=' + this.get_data('id'),
                            type: 'put',
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.is_show = 0;
                                    _this.create_element();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    }, create_code: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var model = this;
                        if (new Model({
                                url: _global.url.api + 'coupop_code',
                                data: {id: id}
                            }).put()) {
                            _this.collection.reload();
                        }
                        ;
                    }, qrcode: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        dialog({
                            title: '优惠券-微信扫一扫领取页面',
                            content: '<div id="show-qrcode"></div>',
                            width: 200,
                            height: 200,
                            modal: true
                        }).show();
                        $('#show-qrcode').qrcode({
                            width: 200,
                            height: 200,
                            //text: _global.url.wapUrl + 'wxlogin?gourl=' + encodeURIComponent(_global.url.wapUrl + 'coupon/receive/' + id)
                            text: _global.url.wapUrl + 'coupon/receive/' + id
                        });
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //查询
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
            //二维码
            $('.site-coupon-qrcode').on('click', function (e) {
                e.preventDefault();
                dialog({
                    title: '优惠券列表-微信扫一扫领取页面',
                    content: '<div id="show-qrcode"></div>',
                    width: 200,
                    height: 200,
                    modal: true
                }).show();
                $('#show-qrcode').qrcode({
                    width: 200,
                    height: 200,
                    //text: _global.url.wapUrl + 'wxlogin?gourl=' + encodeURIComponent(_global.url.wapUrl + 'coupon/site/' + _global.site_info.id)
                    text: _global.url.wapUrl + 'coupon/site/' + _global.site_info.id
                });
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                $('input[name="keyword"]').val('');
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });

        }
    };

    new index();

});